﻿using GrayParrot.Core;
using GrayParrot.ServiceProcess;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;

namespace TestDelayedService
{
    public partial class Service1 : ReliableService<Service1>
    {
        CancellableTasksManager<TestDelayedSubscriber> m_tasksMgr = null;
        TestDelayedSubscriber m_subscriber = null;

        CancellableTasksManager<FakeWorker> m_fakeWorkerTasksMgr = null;
        FakeWorker m_fakeWorkerSubscriber = null;

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            //m_tasksMgr = new CancellableTasksManager<TestDelayedSubscriber>();

            m_fakeWorkerTasksMgr = new CancellableTasksManager<FakeWorker>();


            base.OnStart(args);
        }

        protected override void OnStop()
        {
            base.OnStop();
        }

        protected override void Worker()
        {
            // PUT HERE YOUR WORKER CODE...
            //m_subscriber = new TestDelayedSubscriber();

            m_fakeWorkerSubscriber = new FakeWorker();
            m_fakeWorkerSubscriber.CancellationToken = this.CancellationToken;

            /* NECESSARIO PER CONOSCERE LE ISTANZE DA KILLARE ALLA CANCELLAZIONE DEL THREAD... */
            //m_tasksMgr.AddInstance(m_subscriber);

            m_fakeWorkerTasksMgr.AddInstance(m_fakeWorkerSubscriber);

            /* 
             * OPPURE SE NON SI VUOLE CREARE L'ISTANZA DIRETTAMENTE USIAMO IL METODO FACTORY DELLA CancellableTasksManager 
             * CHE SI PREOCCUPA ANCHE DI SALVARE L'INSTANZA NEL BUFFER...
             
            m_subscriber = m_tasksMgr.Create();
            */

            //m_subscriber.OnConnectionFailed += m_subscriber_OnConnectionFailed;
            //m_subscriber.Init();

            m_fakeWorkerSubscriber.Init();
        }

        void m_subscriber_OnConnectionFailed(object sender, Exception e)
        {
            GrayParrot.Logging.Logger.For<Service1>().Error(e);

            throw e;
        }

        protected override void DisposeWorker()
        {
            // PUT HERE YOUR WORKER CODE DISPOSING...
            /* SE SI HANNO DA GESTIRE PIU' THREADS, MA VA BENE ANCHE QUANDO IL THREAD E' UNO SOLO... */
            //m_tasksMgr.DisposeInstances((x) =>
            //{
            //    x.OnConnectionFailed -= m_subscriber_OnConnectionFailed;
            //    x.Dispose();
            //    x = null;
            //});


            m_fakeWorkerTasksMgr.DisposeInstances((x) =>
            {
                x.Dispose();
                x = null;
            });


            /* QUESTO E' CIO' CHE NORMALMENTE AVREMMO SCRITTO... */
            //if (m_subscriber != null)
            //{
            //    m_subscriber.OnConnectionFailed -= m_subscriber_OnConnectionFailed;
            //    m_subscriber.Dispose();

            //    m_subscriber = null;
            //}
        }
    }
}
